package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Student;
import com.situ.web.util.JDBCUtil;

@WebServlet("/student1")
public class StudentServlet1 extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.service()");
		
//		ArrayList<Student> list = new ArrayList<Student>();
//		Student student1 = new Student(1, "張三1", 23, "男");		
//		Student student2 = new Student(2, "張三2", 23, "男");		
//		Student student3 = new Student(3, "張三3", 23, "男");		
//		Student student4 = new Student(4, "張三4", 23, "男");		
//		Student student5 = new Student(5, "張三5", 23, "男");	
//		list.add(student1);
//		list.add(student2);
//		list.add(student3);
//		list.add(student4);
//		list.add(student5);
		
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender FROM student";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
		
		// 乱码问题
//		resp.setContentType("text/html;charset=utf-8");
//		PrintWriter printWriter = resp.getWriter();
//		printWriter.println("<table border='1' cellspacing='0' width='400px'>");
//		printWriter.println("	<tr>                                         ");
//		printWriter.println("		<td>ID</td>                              ");
//		printWriter.println("		<td>名字</td>                            ");
//		printWriter.println("		<td>年龄</td>                            ");
//		printWriter.println("		<td>性别</td>                            ");
//		printWriter.println("	</tr>                                        ");
//		
//		for (Student student : list) {
//			printWriter.println("	<tr>                                         ");
//			printWriter.println("		<td>"+student.getId()+"</td>                               ");
//			printWriter.println("		<td>"+student.getName()+"</td>                           ");
//			printWriter.println("		<td>"+student.getAge()+"</td>                              ");
//			printWriter.println("		<td>"+student.getGender()+"</td>                              ");
//			printWriter.println("	</tr>                                        ");
//		}
//		
//		printWriter.println("</table>                                        ");
//		printWriter.close();                                                
	}
	
}
